#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>

void detbigend()
{
	int i=0x1234;
	int ch=ntohs(i);
	char *p=(char *) &i;
	printf("define i: %X\n",i);
	printf("the first output value: %X\n",*p);
	printf("ntohs(i) is %X\n",ch);
	char *p1=(char *)&ch;
	printf("ntohs the first value:%X\n",*p1);
	if(ch !=1234)
		printf("define value is not equel ntohs, so it is a little end\n");
else 
	printf("it is a big end\n");
}

int getfile_status()
{
	FILE *fp,*fp1;
	int result;
	int i=9;
	fp=fopen("files/file_status","w");
	if(fp==NULL) return ;
	else
	fprintf(fp,"%d",i);
	fclose(fp);
	fp1=fopen("files/file_status","r");
	if(fp1==NULL) return ;
	else result=fgetc(fp1)-'0';
	fclose(fp1);
	return result;
}

void detdaemon()
{
	if(daemon(1,1) <0)
	{perror("error daemon.....\n");
		exit(1);
	}
	while(1);
}
// get file item  given value like name=helen to get helen
char *get_item(char *file, char *property)
{
	char *token=NULL;
	token=malloc(256);
	char line[1024]={0};
	FILE *fp;
	if((fp=fopen(file,"r"))== NULL)
		return NULL;
	while(fgets(line,1024,fp)){
		if(strstr(line,property) !=NULL)
		{
			token=strstr(line,"=");
			break;
		}
	}
	fclose(fp);
	return token ? token+1: NULL;
}

void sfp_network()
{
#define BUF_LEN 512
    FILE *fp,*sfp1;
    char buffer[BUF_LEN];
    char if_name[128],other_str[256],*ptr;
    unsigned long r_bytes = 0,r_pack = 0,t_bytes = 0,t_pack = 0;
    int r_errs = 0,r_drop = 0,r_fifo = 0,r_fram = 0,r_comp = 0,r_mult = 0;
    if((fp=fopen("files/dev","r")) != NULL)
    {
        memset(buffer,0,BUF_LEN);
        while(fgets(buffer,BUF_LEN,fp)!=NULL){
            if(!strstr(buffer,":"))
                continue;
            ptr = buffer;
            while(*ptr != ':')
                ptr++;
            *ptr = ' ';
            sscanf(buffer,"%s %ld %ld %d %d %d %d %d %d %ld %ld %s",
                    if_name,&r_bytes,&r_pack,&r_errs,&r_drop,&r_fifo,&r_fram,&r_comp,&r_mult,&t_bytes,&t_pack,other_str);
            if(strcmp(if_name,"eth0") == 0){
                if((sfp1=fopen("files/sfp_tmp","w"))!=NULL) {
                    fprintf(sfp1,"%ld\n",r_pack);
                    fclose(sfp1);
                }
            }
        }
        fclose(fp);
    }
}

void network_change()
{

	FILE *fp_sfp;
	char last_status[256]={0};
	char sfp_status[256]={0};
	int sfp_stflag = 0, old_sfp_stflag = 0;
	int i;
	sfp_network();
	system("echo Link down > files/SFP_status");
	//for sfp
	for(i=0;i<3;i++)
	{
		if ((fp_sfp = fopen("files/sfp_tmp", "r")) != NULL) {
			fgets(last_status,sizeof(last_status), fp_sfp);
			fclose(fp_sfp);
		}
		sleep(6);
		sfp_network();
		if ((fp_sfp = fopen("files/sfp_tmp", "r")) != NULL) {
			fgets(sfp_status,sizeof(sfp_status), fp_sfp);
			fclose(fp_sfp);
		}
		FILE *sfp_st=fopen("files/SFP_status","w");
		if(strcmp(sfp_status,last_status) ==0) {
			fprintf(sfp_st,"%s\n","Link down");
			sfp_stflag = 0;
		}
		else {
			fprintf(sfp_st,"%s\n","10G/Full");
			sfp_stflag = 1;
		}
		fclose(sfp_st);
		if(old_sfp_stflag != sfp_stflag) {
			if(sfp_stflag == 1) 
				system("cat files/uptime |awk '{print $1}' > files/SFP_uptime");
		}
		old_sfp_stflag = sfp_stflag;

	}
	unlink("files/sfp_tmp");
	unlink("files/SFP_status");
	unlink("files/SFP_uptime");
}
void show_ip()
{
	char str[INET_ADDRSTRLEN];
	struct sockaddr_in sa;
	inet_pton(AF_INET,"192.168.1.1",&(sa.sin_addr));
	inet_ntop(AF_INET,&(sa.sin_addr),str,INET_ADDRSTRLEN);
	printf("%s\n",str);
}

char *cat_file(char *name)
{
	FILE *fp;
	static char buf[512];
	buf[0]='\0';
	fp=fopen(name,"r");
	if (fp==NULL)
		return buf;
	fgets(buf,sizeof(buf),fp);
	int i=0;
	while(buf[i] !='\0' && buf[i] !='\r' && buf[i] != '\n')
		i++;
	buf[i]='\0';
	return buf;
}
int main()
{
	char *name=NULL;
	name=malloc(1024);
	name=get_item("files/helen","user_name=");
	printf("user name is %s",name);
	char tmp_name[1024];
	strncpy(tmp_name,name,strlen(name));
	printf("tmp name is %s",tmp_name);
	detbigend();
//	detdaemon();
	int status=getfile_status();
	printf("status is %d\n",status);
	system("ls");
	network_change();
	system("ls");
	show_ip();
	char *c_status=NULL;
	c_status=cat_file("files/LAN_status");
	printf("%s\n",c_status);
	if(strcmp(c_status,"Link down")==0)
		printf("status is down\n");
	char host[512];
	strncpy(host,c_status,sizeof(host));
		printf("status is %s\n",host);
	return 0;
}
